﻿using System;
using System.Web;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Pages
{
    public partial class UploadProductImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //CHECK THE FREAKING HEADERS IN WEB.CONFIG

            Response.ContentType = "text/plain";
            Response.Cache.SetAllowResponseInBrowserHistory(false);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();

            if (!String.IsNullOrEmpty(Request.QueryString["EmployeeToken"]))
            {
                if (PageBaseClass.EmployeeExternalSessionValid(Request.QueryString["EmployeeToken"]))
                {
                    SaveImage();
                    return;
                }
            }

            Response.Write("false");

        }

        private void SaveImage()
        {
            WebsiteProperties Website = WebsiteData.WebsiteDetail();
            string fileName = StringUtils.CleanStringForURL(Request.Files[0].FileName);
            string file = "/Files/ProductImageUploads/" + fileName;
            Request.Files[0].SaveAs(Server.MapPath(file));

            ImageUtils.CreateImageCache("/Files/ProductImageUploads/", "/ProductImagesCache/", fileName, Website.LargeImageSize.Value, Website.MediumImageSize.Value, Website.SmallImageSize.Value);

            Response.Write("true");

        }

    }
}